package com.tvunetworks.device.extra.service.impl;

import com.tvunetworks.center.common.exception.ServerException;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.User;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.gs.model.Device;
import com.tvunetworks.device.extra.constants.ResultConstant;
import com.tvunetworks.device.extra.model.param.UpdateExtParam;
import com.tvunetworks.device.extra.service.ExtService;
import com.tvunetworks.device.extra.service.UserService;
import com.tvunetworks.device.extra.service.feign.ChannelFeignService;
import com.tvunetworks.device.extra.service.feign.DeviceFeignService;
import com.tvunetworks.device.extra.service.feign.UserFeignService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Etan Yang
 * @create: 2021/11/19 14:13
 */

@Service
@Slf4j
public class ExtServiceImpl implements ExtService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private DeviceFeignService deviceFeignService;
    @Autowired
    private ChannelFeignService channelFeignService;

    @Override
    public String addExtToUser(String userId, String sourceName, String sourceType, String url, String rid) {
        //1.get loginUser message By userId from cc user Service
        LoginUser loginUser = userService.getLoginUserByUserIdFromCcUserService(userId);
        //2.if user not exist in CC service, get user message form userGroup service, and create user in CC Service,and role is 'user'
        if (loginUser == null) {
            User user = userService.getUserByUserIdFromUserGroup(userId, Boolean.TRUE);
            //create user in CC service
            userService.createLoginUserByUserIdAndEmail(userId, user.getEmail());
        }
        //3.add ext to user
        if (StringUtils.equals("Rtmp", sourceType)) {
            sourceType = "Ext";
        }
        Result<String> extAndAddToUser = userFeignService.createExtAndAddToUser(userId, sourceName, url, sourceType);
        if (!extAndAddToUser.assertSuccess()) {
            log.error("addExtToUser>>>>>>>addExt to user failed,userId:[{}],sourceName:[{}],sourceType:[{}],url:[{}],result:[{}]",
                    userId, sourceName, sourceType, url, extAndAddToUser);
            throw new ServerException(ResultConstant.User.ADD_EXT_TO_USER_FAILED);
        }
        String sourceId = extAndAddToUser.getResult();
        //4.send ext to receiver
        if (StringUtils.isNotEmpty(rid)) {
            Device device = deviceFeignService.getCurrentDevice(rid);
            if (device == null) {
                log.error("addExtToUser>>>>>device is not exist, rid:[{}]", rid);
                throw new ServerException(ResultConstant.Device.DEVICE_IS_NOT_EXIST);
            }
            Result<String> stringResult = deviceFeignService.addExtToReceiver(rid, sourceId);
            if (!stringResult.assertSuccess()) {
                log.error("addExtToUser>>>>>>>addExt to user failed,userId:[{}],sourceName:[{}],sourceType:[{}],url:[{}],sourceId:[{}],result:[{}]",
                        userId, sourceName, sourceType, url, sourceId, stringResult);
            }
        }
        return sourceId;
    }

    @Override
    public Result updateExt(UpdateExtParam param) {
        String remark = StringUtils.isEmpty(param.getRemark()) ? "Device_Extra service" : param.getRemark();
        return userFeignService.updateDeviceExt(param.getSourceId(), param.getSourceName(), param.getUrl(), remark);
    }

    @Override
    public Result deleteExt(String sourceId, String userId) {
        //1.删除channel中的event
        channelFeignService.setPlaceholderBySourceId(userId, sourceId, null);
        return userFeignService.deleteExt(sourceId);
    }
}
